package com.tedu.pj.sys.service.impl;

import com.tedu.pj.common.exception.ServiceException;
import com.tedu.pj.common.pojo.PageObject;
import com.tedu.pj.sys.dao.SysLogDao;
import com.tedu.pj.sys.pojo.SysLog;
import com.tedu.pj.sys.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author Song
 * @since 2020/10/15
 */
@Service
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    SysLogDao sysLogDao;

    @Override
    public PageObject<SysLog> findPageObjects(String username, Integer pageCurrent) {
        if (pageCurrent == null || pageCurrent < 1) {
            throw new IllegalArgumentException("当前页码不正确");
        }
        int rowCount = sysLogDao.getRowCount(username);
        if (rowCount == 0) {
            throw new ServiceException("系统没有查到对应记录");
        }
        int pageSize = 10;
        int startIndex = (pageCurrent - 1) * pageSize;
        List<SysLog> records = sysLogDao.findPageObjects(username, startIndex, pageSize);

        return new PageObject(pageCurrent, rowCount, pageSize, records);
    }

    @Override
    public int deleteObjects(Integer... ids) {
        if (ids == null || ids.length == 0) {
            throw new IllegalArgumentException("请选择一个");
        }
        int rows;
        try {
            rows = sysLogDao.deleteObjects(ids);
        } catch (Throwable e) {
            throw new ServiceException("系统故障，正在恢复中...");
        }
        if (rows == 0) {
            throw new ServiceException("记录可能已经不存在");

        }
        return rows;
    }

    @Override
    public void saveObject(SysLog entity) {
        System.out.println(Thread.currentThread().getName());
        sysLogDao.insertObject(entity);
    }
}
